<?php
namespace app\manage\controller;
class ChannelStatus extends Base
{
    //列表
    public function index(){
        if( request()->isAjax() ){
            $page = input('page');
            $limit = input('limit');
            $gameId = input('game_id');
            $cpsId = input('channel_id');
            $id = input('id');

            $where = 1;
            !empty($gameId) && $where .= " && b.game_id={$gameId}";
            !empty($cpsId) && $where .= " && bd.cps_id={$cpsId}";
            !empty($id) && $where .= " && bd.id={$id}";

            if( ! $count=model('ChannelBagDetail')->alias('bd')->join('sdk_channel_bag b', 'b.id=bd.bag_id', 'LEFT')->field('bd.id, bd.cps_id, bd.give_num, bd.get_time, bd.status, b.game_id, b.name')->where($where)->count() ){
                $this->layTable();
            }

            $select = model('ChannelBagDetail')
                ->alias('bd')
                ->join('sdk_channel_bag b', 'b.id=bd.bag_id', 'LEFT')
                ->field('bd.id, bd.cps_id, bd.give_num, bd.get_time, bd.status, b.game_id, b.name')
                ->where($where)
                ->order('bd.get_time desc')
                ->limit(($page-1)*$limit, $limit)
                ->select();

            $data = [];
            $channelIdName = model('Channel')->channelIdName();
            $gameIdName = model('Game')->gameIdName();
            foreach ($select as $k=>$v){
                $data[] = [
                    'id' => $v['id'],
                    'cps_id' => $v['cps_id'],
                    'cps_name' => $channelIdName[ $v['cps_id'] ],
                    'game_id' => $v['game_id'],
                    'game_name' => isset($gameIdName[ $v['game_id'] ]) ? $gameIdName[ $v['game_id'] ] : '',
                    'name' => $v['name'],
                    'give_num' => $v['give_num'],
                    'get_time' => date('Y-m-d H:i:s', $v['get_time']),
                    'status' => $v['status'] ? '已领取' : '未领取',
                ];
            }
            $this->layTable(0, '游戏礼包状态列表', $data, $count);
        }else{
            return view();
        }
    }

    //编辑
    public function edit(){
        if( request()->isAjax() ){
            $save['id'] = input('id');
            $save['status'] = 0;
            $editRs = model('ChannelBagDetail')->allowField(true)->isUpdate(true)->save( $save );
            $editRs!==false ? $this->layJson(0, '编辑成功') : $this->layJson(1, '编辑失败，请重试');
        }else{
            return view();
        }
    }

}